﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Xml.Linq;

namespace Pap.Services.Vactions
{
    /// <summary>
    /// 
    /// </summary>
    class VacationService : IVacationService
    {
        /// <summary>
        /// Gets the vacation file path.
        /// </summary>
        private string VacationFilePath
        {
            get
            {
                return HostingEnvironment.MapPath(
                    ConfigurationManager.AppSettings["VacationFilePath"]);
            }
        }
        /// <summary>
        /// Inserts the vacation date.
        /// </summary>
        /// <param name="vacationDate">The vacation date.</param>
        public void InsertVacationDate(DateTime vacationDate)
        {
            vacationDate = vacationDate.Date.AddHours(9);

            XDocument vacDocument = XDocument.Load(VacationFilePath);

            //find month
            XElement monthElment = vacDocument.Element("months").Descendants("month").Where(
                month => month.Attribute("number").Value.Equals(vacationDate.Month.ToString())).FirstOrDefault();

            //insert vacationDate 
            if (monthElment != null)
            {
                if (!monthElment.Descendants("vacationDate").Any(
                      date => DateTime.Parse(date.Value).Day.Equals(vacationDate.Day)))
                {
                    XElement newVacationDate = new XElement("vacationDate", vacationDate);
                    monthElment.Add(newVacationDate);
                    vacDocument.Save(VacationFilePath);
                }
            }

        }

        /// <summary>
        /// Gets the vacations by month.
        /// </summary>
        /// <param name="monthNumber">The month number.</param>
        /// <returns></returns>
        public IEnumerable<DateTime> GetVacationsByMonth(int monthNumber)
        {
            XDocument vacDocument = XDocument.Load(VacationFilePath);

            XElement monthEl = vacDocument.Element("months").Descendants("month").Where(
                month => month.Attribute("number").Value.Equals(monthNumber.ToString())).FirstOrDefault();

            if (monthEl != null)
            {
                return monthEl.Descendants("vacationDate").Select(
                    vacDate => DateTime.Parse(vacDate.Value));
            }
            return new List<DateTime>();
        }

        /// <summary>
        /// Deletes the vacation.
        /// </summary>
        /// <param name="vacationDate">The vacation date.</param>
        public void DeleteVacation(DateTime vacationDate)
        {
            XDocument vacDocument = XDocument.Load(VacationFilePath);

            XElement monthElment = vacDocument.Element("months").Descendants("month").Where(
                 month => month.Attribute("number").Value.Equals(vacationDate.Month.ToString())).FirstOrDefault();

            if (monthElment != null)
            {
                var vacElment = monthElment.Descendants("vacationDate").Where(
                      date => DateTime.Parse(date.Value).Day.Equals(vacationDate.Day)).FirstOrDefault();

                if (vacElment != null)
                {
                    vacElment.Remove();
                    vacDocument.Save(VacationFilePath);
                }
            }
        }

        /// <summary>
        /// Gets the allvacation days.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <returns></returns>
        public int getAllvacationDays(DateTime startDate, DateTime endDate)
        {
            XDocument vacDocument = XDocument.Load(VacationFilePath);

            var allVacations = vacDocument.Element("months").Descendants("month").SelectMany(
                month => month.Descendants("vacationDate").Select(date => DateTime.Parse(date.Value)));

            var vacationsWithinPeriod = allVacations.Where(date => date.Date >= startDate.Date &&
                date.Date <= endDate.Date);

            int weekendsCount = 0;
            while (startDate <= endDate)
            {
                if (startDate.DayOfWeek == DayOfWeek.Friday || startDate.DayOfWeek == DayOfWeek.Saturday)
                    weekendsCount++;
                startDate = startDate.AddDays(1);
            }

            return weekendsCount + vacationsWithinPeriod.Count();
        }
    }
}
